package cn.bopomo.dealer.base.util;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Iterator;

/**
 * 
 * <p>
 * 类名:Page
 * </p>
 * 
 * <p>
 * 描述:通用分页类，用来封装后台数据库返回对象信息，并在前台显示分页信息
 * </p>
 * 
 * <p>
 * 作者：甘桂敏 goodsuperstar#yahoo.com.cn
 * </p>
 * 
 * @author GanGuimin
 * @version 2.2
 * @since JDK1.4.2
 */
public class Page {
    /**
     * 当前是第几页
     */
    private int curPage = 1; // 当前是第几页

    /**
     * 一共有多少页
     */
    private int maxPage = 1; // 一共有多少页

    /**
     * 一共有多少条记录（行）
     */
    private int maxRowCount = 0; // 一共有多少行

    /**
     * 每页显示多少行
     */
    private int rowsPerPage = 5; // 每页显示多少行

    /**
     * 当前页面要显示的数据列表（包含数据库返回对象的列表）
     */
    private List list = null; // 当前页面显示的数据

    /**
     * 提交的表单的名称
     */
    private String formName = "pageForm"; // 提交的表单名称

    /**
     * 提交表单的附加参数值
     */
    private String parameterValue = null;

    /**
     * 提交表单的附加参数名
     */
    private String parameterName = null;

    /**
     * 提交表单的附加参数表
     */
    private Map parameterMap = null;

    /**
     * 显示分页的前台页面路径名称
     */
    private String target = "";

    /**
     * 定义显示分页信息的格式
     */
    private String styleClass = "";

    /**
     * 表单要在同一页面显示了多少次，初始为1，如果多次调用显示分页HTML代码，该值累加1
     */
    private int showTimes = 1;

    /**
     * 指定分页显示风格
     */
    private int mode = 0;

    /**
     * 默认每页显示记录数
     */
    private static int ROWS_PER_PAGE = 15;
    
    /**
     * 指定是否用作AJAX上
     */
    private boolean isAJAX = false;
    
    /**
     * 指定javascript函数名
     */
    private String javascriptFunctionName = "gotoPage";



	/**
     * 分页信息初始化，每页显示记录数使用默认值
     * 
     */
    public Page() {
        this.curPage = 1;
        this.maxPage = 1;
        this.maxRowCount = 0;
        this.showTimes = 1;
        this.styleClass = "";
        this.rowsPerPage = ROWS_PER_PAGE;
    }
    
    public Page(List objList){
    	if (objList == null){
    		this.curPage = 1;
            this.maxPage = 1;
            this.maxRowCount = 0;
            this.showTimes = 1;
            this.styleClass = "";
            this.rowsPerPage = ROWS_PER_PAGE;
    		return;
    	}
    		
    	this.maxRowCount = objList.size();
    	this.curPage = 1;
    	this.showTimes = 1;
    	this.styleClass = "";
    	this.rowsPerPage = ROWS_PER_PAGE;
    	this.initialize();
    	
    }
    
    public Page(List objList, int rowsPerPage){
    	if (objList == null || rowsPerPage<=0){
    		this.curPage = 1;
            this.maxPage = 1;
            this.maxRowCount = 0;
            this.showTimes = 1;
            this.styleClass = "";
            this.rowsPerPage = ROWS_PER_PAGE;
    		return;
    	}
    		
    	this.maxRowCount = objList.size();
    	this.curPage = 1;
    	this.showTimes = 1;
    	this.styleClass = "";
    	this.rowsPerPage = rowsPerPage;
    	this.initialize();
    	
    }

    /**
     * <p>
     * 设置当前分页页面要显示的数据对象列表
     * </p>
     * 
     * <p>
     * 该对象可以是一个实现了 List 接口的任意列表
     * </p>
     * 
     * @param list -
     *            实现了 List 接口的任意列表
     */
    public void setList(List list) {
        this.list = list;
    }

    /**
     * 获取要在当前分页页面显示的数据对象列表
     * 
     * @return 要显示在前台页面的数据对象列表
     */
    public List getList() {
        return this.list;
    }

    /**
     * 设置记录的总行数（即所有满足条件的纪录总数）
     * 
     * @param maxRowCount -
     *            记录的总行数
     */
    public void setMaxRowCount(int maxRowCount) {
        this.maxRowCount = maxRowCount;
    }

    /**
     * 获取记录的最大行数
     * 
     * @return 满足条件的所有记录条数
     */
    public int getMaxRowCount() {
        return this.maxRowCount;
    }

    /**
     * 设置当前页数（即要显示在前台的当前页数）
     * 
     * @param curPage -
     *            当前页数
     */
    public void setCurPage(int curPage) {
        this.curPage = curPage;
    }

    /**
     * 返回在前台显示的当前页数
     * 
     * @return 当前页数
     */
    public int getCurPage() {
        return this.curPage;
    }

    /**
     * <p>
     * 设置每页显示的记录条数
     * </p>
     * <p>
     * 注意：如果每页显示的记录条数小于等于 0 ，则每页显示的记录条数将置为默认值
     * </p>
     * 
     * @param rows -
     *            每页显示的记录条数
     */
    public void setRowsPerPage(int rows) {
        if (rows > 0) {
            this.rowsPerPage = rows;
        } else {
            this.rowsPerPage = ROWS_PER_PAGE;
        }
    }

    /**
     * 设置页面提交表单的名称,默认为 pageForm，在一般情况下，使用默认值就能正常使用
     * 
     * @param formName -
     *            提交表单的名称
     */
    public void setFormName(String formName) {
        this.formName = formName;
    }

    /**
     * 设置显示分页的前台页面路径名称
     * 
     * @param target -
     *            分页页面路径名称
     */
    public void setTarget(String target) {
        this.target = target;
    }

    /**
     * 设置附加参数的值
     * 
     * @param parameterValue -
     *            附加参数的值
     */
    public void setParameterValue(String parameterValue) {
        if (parameterValue == null) {
            return;
        }

        this.parameterValue = (!parameterValue.trim().equals("")) ? parameterValue
                .trim()
                : null;
    }

    /**
     * 设置附加参数名称
     * 
     * @param parameterName -
     *            附加参数的名称
     */
    public void setParameterName(String parameterName) {
        if (parameterName == null) {
            return;
        }
        this.parameterName = (!parameterName.trim().equals("")) ? parameterName
                .trim() : null;
    }

    /**
     * 设置附加参数表
     * 
     * @param parameterMap
     *            Map 附加参数表
     */
    public void setParameterMap(Map parameterMap) {
        this.parameterMap = parameterMap;
    }

    /**
     * 获取每页显示的记录条数
     * 
     * @return 每页显示的记录条数
     */
    public int getRowsPerPage() {
        return this.rowsPerPage;
    }

    /**
     * 根据记录总数和每页显示的记录条数，计算总共有多少页
     * 
     */
    public void initialize() {
        if (maxRowCount % rowsPerPage == 0) {
            maxPage = maxRowCount / rowsPerPage;
        } else {
            maxPage = this.maxRowCount / rowsPerPage + 1;
        }
    }

    /**
     * 获取根据记录总数和每页显示的记录数计算得出的总页数
     * 
     * @return 记录总页数
     */
    public int getMaxPage() {
        return this.maxPage;
    }

    /**
     * 获取前台分页页面提交表单的名称
     * 
     * @return 提交表单的名称
     */
    public String getFormName() {
        return this.formName;
    }

    /**
     * 获取前台分页页面的路径名称
     * 
     * @return 前台分页页面的路径名称
     */
    public String getTarget() {
        return this.target;
    }

    /**
     * 获取附加的参数值
     * 
     * @return 附加参数的值
     */
    public String getParameterValue() {
        return parameterValue;
    }

    /**
     * 获取附加参数的名称
     * 
     * @return 附加参数的名称
     */
    public String getParameterName() {
        return parameterName;
    }

    /**
     * 获取附加参数的列表
     * 
     * @return Map 附加参数Map
     */
    public Map getParameterMap() {
        return parameterMap;
    }

    /**
     * 获取默认每页显示的记录条数
     * 
     * @return 默认每页显示的记录条数
     */
    public int getDefaultRowsPerPage() {
        return ROWS_PER_PAGE;
    }

    /**
     * 根据前台分页页面名称和相关分页信息，获取前台页面提交表单和页面信息的 HTML 代码
     * 
     * @param aUrl -
     *            前台分页页面的路径名称
     * @return 前台页面提交表单和页面信息的 HTML 代码
     */
    public String getPageStr(String aUrl) {

        String strFirstPage = "首页";
        String strLastPage = "末页";
        String strNextPage = "下一页";
        String strPrevPage = "上一页";
        String strCurrentPage = "页次";
        String strGoto = "转至：";
        String strInvalidColor = "#930909";
        String strValidColor = "#003399";
        String javascriptUrlParameter = "";

        StringBuffer strPages = new StringBuffer();
        
        if(this.isAJAX == false){
	        // 跳转脚本
	        strPages.append("<script language=\"JavaScript\">\r\n");
	        strPages.append("<!--\r\n");
	        strPages.append("function Jumping(){ \r\n\t");
	        strPages.append("document." + formName + ".submit();\r\n\t");
	        strPages.append("return ;\r\n");
	        strPages.append("}\r\n");
	
	        strPages.append("function " + javascriptFunctionName + "(pagenum){ \r\n\t");
	        strPages.append("document." + formName
	        		+ ".pageSize.value = pagenum;\r\n\t");
	        strPages.append("document." + formName
	                + ".pageSize.value = pagenum;\r\n\t");
	        strPages.append("document." + formName + ".submit();\r\n\t");
	        strPages.append("return ;\r\n");
	        strPages.append("}\r\n");
	        strPages.append("-->\r\n");
	        strPages.append("\r\n</script>\r\n");
	
	        strPages.append("<table border=\"0\" align=\"center\">\r\n");
	
	        strPages.append("<form id=\"" + formName + "\" name=\"" + formName + "\" action=\"" + aUrl
	                + "\" method=\"post\">\r\n");
	
	        strPages.append("<tr>\r\n\t");
        }

        // 判断是否有另外的附加参数
        if (parameterValue != null && parameterName != null) {
        	//判断是否用于AJAX
        	if(this.isAJAX == false){
        		strPages.append("<input type=\"hidden\" name=\"" + parameterName
        				+ "\" value=\"" + parameterValue + "\"/>\r\n\t");
        	}else{
        		javascriptUrlParameter += "&" + parameterName	+ "=" + parameterValue ;
        	}
        }

        if (parameterMap != null && parameterMap.size() != 0) {
            Set keySet = parameterMap.keySet();
            Iterator iter = keySet.iterator();
            while (iter.hasNext()) {
                String paramerName = (String) iter.next();
                if(this.isAJAX == false){
                	strPages.append("<input type=\"hidden\" name=\"" + paramerName
                			+ "\" value=\"" + parameterMap.get(paramerName)
                			+ "\"/>\r\n\t");
                }else{
                	javascriptUrlParameter += "&" + paramerName	+ "=" + parameterMap.get(paramerName) ;
                }
            }
        }

        strPages.append("<td>\r\n\t\t");

        strPages.append("<b>共 <font color=\"" + strInvalidColor + "\">"
                + this.getMaxRowCount() + "</font> 条记录</b>&nbsp;\r\n\t");

    
        
        if (getMaxPage() > 1 && getMaxRowCount() > 1) {
        	if (getCurPage() != 1) {
        		 if(this.isAJAX == false){
        			 strFirstPage = "<a href=\"javascript:" 
        				 		  	 + javascriptFunctionName	
        				 		  	 + "(" 
						 			 + "1" 
			        				 + ")\">" 
			        				 + strFirstPage 
			        				 + "</a>";
        			 strPrevPage = 	"<a href=\"javascript:" 
						 		  	 + javascriptFunctionName	
						 		  	 + "("  
			        				 + (this.getCurPage()-1) 
			        				 + ")\">" 
			        				 + strPrevPage 
			        				 + "</a>";
        		 }else{
	        		strFirstPage = 	"<a href=\"javascript:" 
									+ javascriptFunctionName	
									+ "('" 
									+ aUrl + "?pageSize=1" 
									+ javascriptUrlParameter
									+ "')\">" 
									+ strFirstPage 
									+ "</a>";
	        		strPrevPage = 	"<a href=\"javascript:" 
					    			+ javascriptFunctionName	
					    			+ "('" 
					    			+ aUrl + "?pageSize=" 
					    			+ (this.getCurPage()-1) 
					    			+ javascriptUrlParameter
					    			+ "')\">" 
					    			+ strPrevPage 
					    			+ "</a>";
        		 }
        	}
        	if (getCurPage() != getMaxPage()) {
        		if(this.isAJAX == false){
	       			 strNextPage = 	"<a href=\"javascript:" 
						 		  	 + javascriptFunctionName	
						 		  	 + "("  
				       				 + (this.getCurPage()+1) 
				       				 + ")\">" 
				       				 + strNextPage 
				       				 + "</a>";
	       			strLastPage = "<a href=\"javascript:" 
						 		  	 + javascriptFunctionName	
						 		  	 + "("   
				       				 + this.getMaxPage()  
				       				 + ")\">" 
				       				 + strLastPage 
				       				 + "</a>";
        		} else{
	       			 strNextPage = "<a href=\"javascript:" 
				       				 + javascriptFunctionName	
				       				 + "('" 
				       				 + aUrl + "?pageSize=" 
				       				 + (this.getCurPage()+1) 
				       				 + javascriptUrlParameter
				       				 + "')\">" 
				       				 + strNextPage 
				       				 + "</a>";
	       			 strLastPage = "<a href=\"javascript:" 
				       				 + javascriptFunctionName	
				       				 + "('" 
				       				 + aUrl + "?pageSize=" 
				       				 + this.getMaxPage() 
				       				 + javascriptUrlParameter
				       				 + "')\">" 
				       				 + strLastPage 
				       				 + "</a>";
       		 	}
        	}
        }
        
	      strPages.append("\t<font face=\"webdings\" color=\""
	    	      + strInvalidColor + "\">9</font><font color=\""
	    	      + strInvalidColor + "\"><b>" + strFirstPage
	    	      + "</b></font>\r\n\t");
	    	      strPages.append("\t<font face=\"webdings\" color=\""
	    	      + strInvalidColor + "\">7</font><font color=\""
	    	      + strInvalidColor + "\"><b>" + strPrevPage
	    	      + "</b></font>\r\n\t");
	    	      strPages.append("\t<font color=\"" + strInvalidColor + "\"><b>"
	    	      + strNextPage
	    	      + "</b></font><font face=\"webdings\" color=\""
	    	      + strInvalidColor + "\">8</font>\r\n\t");
	    	      strPages.append("\t<font color=\"" + strInvalidColor + "\"><b>"
	    	      + strLastPage
	    	      + "</b></font><font face=\"webdings\" color=\""
	    	      + strInvalidColor + "\">:</font>\r\n\t");

      
//        if (getMaxPage() == 1 || getMaxRowCount() == 0) {//当前为"只有一页"
//            strPages.append("\t<font face=\"webdings\" color=\""
//                    + strInvalidColor + "\">9</font><font color=\""
//                    + strInvalidColor + "\"><b>" + strFirstPage
//                    + "</b></font>\r\n\t");
//            strPages.append("\t<font face=\"webdings\" color=\""
//                    + strInvalidColor + "\">7</font><font color=\""
//                    + strInvalidColor + "\"><b>" + strPrevPage
//                    + "</b></font>\r\n\t");
//            strPages.append("\t<font color=\"" + strInvalidColor + "\"><b>"
//                    + strNextPage
//                    + "</b></font><font face=\"webdings\" color=\""
//                    + strInvalidColor + "\">8</font>\r\n\t");
//            strPages.append("\t<font color=\"" + strInvalidColor + "\"><b>"
//                    + strLastPage
//                    + "</b></font><font face=\"webdings\" color=\""
//                    + strInvalidColor + "\">:</font>\r\n\t");
//        //*****************************************************************************************************************************
//        //*****************************************************************************************************************************
//        //*****************************************************************************************************************************
//        } else if (getMaxPage() > 1 && getCurPage() == 1) {//当前页为"首页"
//            strPages.append("\t<font face=\"webdings\" color=\""
//                    + strInvalidColor + "\">9</font><font color=\""
//                    + strInvalidColor + "\"><b>" + strFirstPage
//                    + "</b></font>\r\n\t");
//            strPages.append("\t<font face=\"webdings\" color=\""
//                    + strInvalidColor + "\">7</font><font color=\""
//                    + strInvalidColor + "\"><b>" + strPrevPage
//                    + "</b></font>\r\n\t");
//            strPages.append("\t<a href=\"javascript:gotoPage("
//                    + (getCurPage() + 1) + ")\"><font color=\"" + strValidColor
//                    + "\"><b>" + strNextPage
//                    + "</b></font></a><font face=\"webdings\">8</font>\r\n\t");
//            strPages.append("\t<a href=\"javascript:gotoPage(" + getMaxPage()
//                    + ")\"><font color=\"" + strValidColor + "\"><b>"
//                    + strLastPage
//                    + "</b></font></a><font face=\"webdings\">:</font>\r\n\t");
//        //*****************************************************************************************************************************
//        //*****************************************************************************************************************************
//        //*****************************************************************************************************************************
//        } else if (getMaxPage() > 1 && getCurPage() < getMaxPage()) {//当前页为"中间页"
//            strPages
//                    .append("\t<font face=\"webdings\">9</font><a href=\"javascript:gotoPage(1)\"><font color=\""
//                            + strValidColor
//                            + "\"><b>"
//                            + strFirstPage
//                            + "</b></font></a>\r\n\t");
//            strPages
//                    .append("\t<font face=\"webdings\">7</font><a href=\"javascript:gotoPage("
//                            + (getCurPage() - 1)
//                            + ")\"><font color=\""
//                            + strValidColor
//                            + "\"><b>"
//                            + strPrevPage
//                            + "</b></font></a>\r\n\t");
//            strPages.append("\t<a href=\"javascript:gotoPage("
//                    + (getCurPage() + 1) + ")\"><font color=\"" + strValidColor
//                    + "\"><b>" + strNextPage
//                    + "</b></font></a><font face=\"webdings\">8</font>\r\n\t");
//            strPages.append("\t<a href=\"javascript:gotoPage(" + getMaxPage()
//                    + ")\"><font color=\"" + strValidColor + "\"><b>"
//                    + strLastPage
//                    + "</b></font></a><font face=\"webdings\">:</font>\r\n\t");
//        //*****************************************************************************************************************************
//        //*****************************************************************************************************************************
//        //*****************************************************************************************************************************
//        } else if (getMaxPage() > 1 && getCurPage() == getMaxPage()) {//当前页为"最后一页"
//            strPages
//                    .append("\t<font face=\"webdings\">9</font><a href=\"javascript:gotoPage(1)\"><font color=\""
//                            + strValidColor
//                            + "\"><b>"
//                            + strFirstPage
//                            + "</b></font></a>\r\n\t");
//            strPages
//                    .append("\t<font face=\"webdings\">7</font><a href=\"javascript:gotoPage("
//                            + (getCurPage() - 1)
//                            + ")\"><font color=\""
//                            + strValidColor
//                            + "\"><b>"
//                            + strPrevPage
//                            + "</b></font></a>\r\n\t");
//            strPages.append("\t<font color=\"" + strInvalidColor + "\"><b>"
//                    + strNextPage
//                    + "</b></font><font face=\"webdings\" color=\""
//                    + strInvalidColor + "\">8</font>\r\n\t");
//            strPages.append("\t<font color=\"" + strInvalidColor + "\"><b>"
//                    + strLastPage
//                    + "</b></font><font face=\"webdings\" color=\""
//                    + strInvalidColor + "\">:</font>\r\n\t");
//        }

        // strPages.append("\t<font
        // color="+strValidColor+"><b>转至：</b></font>\r\n\t\t<input
        // type='text' name='page' size=4 maxlength=4 value=" +
        // getCurPage() +
        // " onKeyDown = \"javaScript:return dFilter(event.keyCode,this);\"" +
        // ">" +
        // "\r\n\t\t<input type='submit' value='Go' name='cndok'>\r\n\t");
        // ----------------------------------------------------------------------]
        // 显示 n 条记录(行)/页
        // strPages.append("\t&nbsp;<b><font color="+strInvalidColor+">" +
        // getRowsPerPage()
        // +
        // "</font> 条记录(行)/页</b>\r\n\t");
        // ----------------------------------------------------------------------
	 		//下拉跳转框
	 		strPages.append("\t<font color=\""
                    + strValidColor
                    + "\"><b>"
                    + strGoto
                    + "</b></font>\r\n\t\t" +
                    "<select name=\"pageSize\" ");
	 		if(this.isAJAX == false){
	 			strPages.append("onchange=\"javascript:Jumping()\">");
	 		}else{
	 			strPages.append("onchange=\"javascript:" 
	 	    			+ javascriptFunctionName	
	 	    			+ "('" 
	 	    			+ aUrl + "?pageSize='+this.value+'" 
	 	    			+ javascriptUrlParameter
	 	    			+ "')\">");
	 		}
	 		
	        for (int i = 1; i <= this.getMaxPage(); i++) {
	        	strPages.append("\r\n\t\t\t<option");
	            if (i == this.getCurPage()) {
	            	strPages.append(" selected");
	            }
	            strPages.append(" value=\"" + i + "\">第 " + i + " 页</option>");
	        }
	        strPages.append("\r\n\t\t</select>\r\n\t");
	 	
        strPages.append("\t<font color=\"" + strValidColor + "\"><b>"
                + strCurrentPage + "：</b></font><font color=\""
                + strInvalidColor + "\"><b>" + getCurPage()
                + "</b></font>\r\n\t\t<font color=\"" + strValidColor
                + "\"><b>/</b></font>\r\n\t\t<font color=\"" + strInvalidColor
                + "\"><b>" + getMaxPage() + "</b></font><font color=\""
                + strValidColor + "\"><b> 页 </b></font>");

        strPages.append("\r\n\t</td>\r\n</tr>\r\n" );
        if(this.isAJAX == false){
        	strPages.append("</form>\r\n");
        }
        strPages.append("</table>");

        return strPages.toString();
    }

    private String getPageHtml(String aURL, int mode) {
//        String recordCountTip = "Record Count";
//        String currentPageTip = "Current Page/Page Count";
//        String firstPage = "First Page";//首页
//        String prevPage = "Prev Page";//上一页
//        String nextPage = "Next Page";//下一页
//        String lastPage = "Last Page";//末页
//        String pages ="Pages: ";//页码
//        String prev_10_Pages = "Prev 10 Pages";//上10页
//        String next_10_Page = "Next 10 Pages";//下10页
//        String no_Pages = "No Pages";//无分页
//        String input_Page = "Input page";//输入页码
        String recordCountTip = "记录数";
        String currentPageTip = "当前页码/总页码";
        String firstPage = "首页";//首页
        String prevPage = "上一页";//上一页
        String nextPage = "下一页";//下一页
        String lastPage = "末页";//末页
        String pages ="页码: ";//页码
        String prev_10_Pages = "前十页";//上10页
        String next_10_Page = "后十页";//下10页
        String no_Pages = "无";//无分页
        String input_Page = "输入页码";//输入页码
        
        
        String strHtml = "";
        switch (mode) {
        case 0: // 模式1 (页数,首页,前页,后页,尾页)
            strHtml += "<span class=\"count\" title =\""+recordCountTip+"\"> " + this.maxRowCount
            + " </span>";
            strHtml += "<span class=\"count\" title=\""+currentPageTip+"\">"+this.curPage + " / "
                    + this.maxPage + "</span>";
            strHtml += "<span class=\"number\">";
            if (this.getPreviousPage() < 1) {
                strHtml += "<span title=\""+firstPage+"\">&#171;</span>";
                strHtml += "<span title=\""+prevPage+"\">&#139;</span>";
            } else {
                strHtml += "<span title=\""+firstPage+"\"><a href=\"javascript:"
                        + this.formName + "_" + this.showTimes
                        + "_toPage(1);\">&#171;</a></span>";
                strHtml += "<span title=\""+prevPage+"\"><a href=\"javascript:"
                        + this.formName + "_" + this.showTimes + "_toPage("
                        + this.getPreviousPage() + ");\">&#139;</a></span>";
            }
            for (int i = 1; i <= this.maxPage; i++) {
                if (i > 0) {
                    if (i == this.curPage) {
                        strHtml += "<span title=\""+pages + i + "\">[" + i
                                + "]</span>";
                    } else {
                        strHtml += "<span title=\""+pages + i
                                + "\"><a href=\"javascript:" + this.formName
                                + "_" + this.showTimes + "_toPage(" + i
                                + ");\">[" + i + "]</a></span>";
                    }
                }
            }
            if (this.getNextPage() > this.maxPage) {
                strHtml += "<span title=\""+nextPage+"\">&#155;</span>";
                strHtml += "<span title=\""+lastPage+"\">&#187;</span>";
            } else {
                strHtml += "<span title=\""+nextPage+"\"><a href=\"javascript:"
                        + this.formName + "_" + this.showTimes + "_toPage("
                        + this.getNextPage() + ");\">&#155;</a></span>";
                strHtml += "<span title=\""+lastPage+"\"><a href=\"javascript:"
                        + this.formName + "_" + this.showTimes + "_toPage("
                        + this.maxPage + ");\">&#187;</a></span>";
            }
            strHtml += "</span><br/>";
            break;
        case 1: // 模式1 (10页缩略,首页,前页,后页,尾页)
            strHtml += "<span class=\"count\" title =\""+recordCountTip+"\"> " + this.maxRowCount
            + " </span>";
            strHtml += "<span class=\"count\" title=\""+currentPageTip+"\">"+ this.curPage + " / "
                    + this.maxPage + "</span>";
            strHtml += "<span class=\"number\">";
            if (this.getPreviousPage() < 1) {
                strHtml += "<span title=\""+firstPage+"\">&#171;</span>";
                strHtml += "<span title=\""+prevPage+"\">&#139;</span>";
            } else {
                strHtml += "<span title=\""+firstPage+"\"><a href=\"javascript:"
                        + this.formName + "_" + this.showTimes
                        + "_toPage(1);\">&#171;</a></span>";
                strHtml += "<span title=\""+prevPage+"\"><a href=\"javascript:"
                        + this.formName + "_" + this.showTimes + "_toPage("
                        + this.getPreviousPage() + ");\">&#139;</a></span>";
            }
            int startPage = 0;
            if (this.curPage % 10 == 0) {
                startPage = this.curPage - 9;
            } else {
                startPage = this.curPage - this.curPage % 10 + 1;
            }
            if (startPage > 10)
                strHtml += "<span title=\""+prev_10_Pages+"\"><a href=\"javascript:"
                        + this.formName
                        + "_"
                        + this.showTimes
                        + "_toPage("
                        + (startPage - 1) + ");\">...</a></span>";
            for (int i = startPage; i < startPage + 10; i++) {
                if (i > this.maxPage)
                    break;
                if (i == this.curPage) {
                    strHtml += "<span title=\""+pages + i + "\">[" + i
                            + "]</span>";
                } else {
                    strHtml += "<span title=\""+pages + i
                            + "\"><a href=\"javascript:" + this.formName + "_"
                            + this.showTimes + "_toPage(" + i + ");\">[" + i
                            + "]</a></span>";
                }
            }
            if (this.maxPage >= startPage + 10)
                strHtml += "<span title=\""+next_10_Page+"\"><a href=\"javascript:"
                        + this.formName
                        + "_"
                        + this.showTimes
                        + "_toPage("
                        + (startPage + 10) + ");\">...</a></span>";
            if (this.getNextPage() > this.maxPage) {
                strHtml += "<span title=\""+nextPage+"\">&#155;</span>";
                strHtml += "<span title=\""+lastPage+"\">&#187;</span>";
            } else {
                strHtml += "<span title=\""+nextPage+"\"><a href=\"javascript:"
                        + this.formName + "_" + this.showTimes + "_toPage("
                        + this.getNextPage() + ");\">&#155;</a></span>";
                strHtml += "<span title=\""+lastPage+"\"><a href=\"javascript:"
                        + this.formName + "_" + this.showTimes + "_toPage("
                        + this.maxPage + ");\">&#187;</a></span>";
            }

            strHtml += "</span>";
            break;
        case 2: // 模式2 (前后缩略,页数,首页,前页,后页,尾页)
            strHtml += "<span class=\"count\" title =\""+recordCountTip+"\"> " + this.maxRowCount
            + " </span>";
            strHtml += "<span class=\"count\" title=\""+currentPageTip+"\">"+this.curPage + " / "
                    + this.maxPage + "</span>";
            strHtml += "<span class=\"number\">";
            if (this.getPreviousPage() < 1) {
                strHtml += "<span title=\""+firstPage+"\">&#171;</span>";
                strHtml += "<span title=\""+prevPage+"\">&#139;</span>";
            } else {
                strHtml += "<span title=\""+firstPage+"\"><a href=\"javascript:"
                        + this.formName + "_" + this.showTimes
                        + "_toPage(1);\">&#171;</a></span>";
                strHtml += "<span title=\""+prevPage+"\"><a href=\"javascript:"
                        + this.formName + "_" + this.showTimes + "_toPage("
                        + this.getPreviousPage() + ");\">&#139;</a></span>";
            }
            if (this.curPage != 1)
                strHtml += "<span title=\""+pages+" 1\"><a href=\"javascript:"
                        + this.formName + "_" + this.showTimes
                        + "_toPage(1);\">[1]</a></span>";
            if (this.curPage >= 5)
                strHtml += "<span>...</span>";
            int endPage = 0;
            if (this.maxPage > this.curPage + 2) {
                endPage = this.curPage + 2;
            } else {
                endPage = this.maxPage;
            }
            for (int i = this.curPage - 2; i <= endPage; i++) {
                if (i > 0) {
                    if (i == this.curPage) {
                        strHtml += "<span title=\""+pages + i + "\">[" + i
                                + "]</span>";
                    } else {
                        if (i != 1 && i != this.maxPage) {
                            strHtml += "<span title=\""+pages + i
                                    + "\"><a href=\"javascript:"
                                    + this.formName + "_" + this.showTimes
                                    + "_toPage(" + i + ");\">[" + i
                                    + "]</a></span>";
                        }
                    }
                }
            }
            if (this.curPage + 3 < this.maxPage)
                strHtml += "<span>...</span>";
            if (this.curPage != this.maxPage)
                strHtml += "<span title=\""+pages + this.maxPage
                        + "\"><a href=\"javascript:" + this.formName + "_"
                        + this.showTimes + "_toPage(" + this.maxPage + ");\">["
                        + this.maxPage + "]</a></span>";
            if (this.getNextPage() > this.maxPage) {
                strHtml += "<span title=\""+nextPage+"\">&#155;</span>";
                strHtml += "<span title=\""+lastPage+"\">&#187;</span>";
            } else {
                strHtml += "<span title=\""+nextPage+"\"><a href=\"javascript:"
                        + this.formName + "_" + this.showTimes + "_toPage("
                        + this.getNextPage() + ");\">&#155;</a></span>";
                strHtml += "<span title=\""+lastPage+"\"><a href=\"javascript:"
                        + this.formName + "_" + this.showTimes + "_toPage("
                        + this.maxPage + ");\">&#187;</a></span>";
            }
            strHtml += "</span><br/>";
            break;
        case 3: // 模式3 (箭头样式,首页,前页,后页,尾页) (only IE)
            strHtml += "<span class=\"count\" title =\""+recordCountTip+"\"> " + this.maxRowCount
            + " </span>";
            strHtml += "<span class=\"count\" title=\""+currentPageTip+"\">"+this.curPage + " / "
                    + this.maxPage + "</span>";
            strHtml += "<span class=\"arrow\">";
            if (this.getPreviousPage() < 1) {
                strHtml += "<span title=\""+firstPage+"\">9</span>";
                strHtml += "<span title=\""+prevPage+"\">7</span>";
            } else {
                strHtml += "<span title=\""+firstPage+"\"><a href=\"javascript:"
                        + this.formName + "_" + this.showTimes
                        + "_toPage(1);\">9</a></span>";
                strHtml += "<span title=\""+prevPage+"\"><a href=\"javascript:"
                        + this.formName + "_" + this.showTimes + "_toPage("
                        + this.getPreviousPage() + ");\">7</a></span>";
            }
            if (this.getNextPage() > this.maxPage) {
                strHtml += "<span title=\""+nextPage+"\">8</span>";
                strHtml += "<span title=\""+lastPage+"\">:</span>";
            } else {
                strHtml += "<span title=\""+nextPage+"\"><a href=\"javascript:"
                        + this.formName + "_" + this.showTimes + "_toPage("
                        + this.getNextPage() + ");\">8</a></span>";
                strHtml += "<span title=\""+lastPage+"\"><a href=\"javascript:"
                        + this.formName + "_" + this.showTimes + "_toPage("
                        + this.maxPage + ");\">:</a></span>";
            }
            strHtml += "</span><br/>";
            break;
        case 4: // 模式4 (下拉框)
            if (this.maxPage < 1) {
                strHtml += "<select name=\"toPage\" disabled>";
                strHtml += "<option value=\"0\">"+no_Pages+"</option>";
            } else {
                String chkSelect = "";
                strHtml += "<select name=\"toPage\" onchange=\""
                        + this.formName + "_" + this.showTimes
                        + "_toPage(this);\">";
                for (int i = 1; i <= this.maxPage; i++) {
                    if (this.curPage == i)
                        chkSelect = " selected=\"selected\"";
                    else
                        chkSelect = "";
                    strHtml += "<option value=\"" + i + "\"" + chkSelect
                            + ">"+pages + i + " / " + this.maxPage
                            + "</option>";
                }
            }
            strHtml += "</select>";
            break;
        case 5: // 模式5 (输入框)
            strHtml += "<span class=\"input\">";
            if (this.maxPage < 1) {
                strHtml += "<input type=\"text\" name=\"toPage\" value=\""+no_Pages+"\" class=\"itext\" disabled=\"disabled\">";
                strHtml += "<input type=\"button\" name=\"go\" value=\"GO\" class=\"ibutton\" disabled=\"disabled\"></option>";
            } else {
                strHtml += "<input type=\"text\" value=\""+input_Page+":\" class=\"ititle\" readonly=\"readonly\">";
                strHtml += "<input type=\"text\" id=\"pageInput"
                        + this.showTimes
                        + "\" value=\""
                        + this.curPage
                        + "\" class=\"itext\" title=\""+input_Page+"\" onkeypress=\"return "
                        + this.formName
                        + "_"
                        + this.showTimes
                        + "_formatInputPage(event);\" onfocus=\"this.select()\">";
                strHtml += "<input type=\"text\" value=\" / " + this.maxPage
                        + "\" class=\"icount\" readonly=\"readonly\">";
                strHtml += "<input type=\"button\" name=\"go\" value=\"GO\" class=\"ibutton\" onclick=\""
                        + this.formName
                        + "_"
                        + this.showTimes
                        + "_toPage(document.getElementById('pageInput"
                        + this.showTimes + "').value);\"></option>";
            }
            strHtml += "</span>";
            break;
        default:
            strHtml = "showPage Error: not find mode " + mode;
            break;
        }
        return strHtml;
    }

    /**
     * <p>
     * 根据Page 类的 target 属性和分页相关信息，获取前台页面提交表单和页面信息的 HTML 代码
     * </p>
     * <p>
     * 注意：使用该方法之前，必须设置 Page 类的 target属性
     * </p>
     * 
     * @return 前台页面提交表单和页面信息的 HTML 代码
     */
    public String getPageStr() {
        return this.getPageStr(target);
    }

    /**
     * <p>
     * 根据Page 类的 target 属性和分页相关信息，获取前台页面提交表单和页面信息的 HTML 代码
     * </p>
     * <p>
     * 注意：使用该方法之前，必须设置 Page 类的 target属性,如果styleClass属性为空，默认为pages，并生成默认的Css样式表
     * </p>
     * 
     * @return 前台页面提交表单和页面信息的 HTML 代码
     */
    public String getPageHtml() {
        StringBuffer pageHtml = new StringBuffer("<!-- 分页区开始 -->");
        /* 判断是否有自定义Style Css 样式表，如果没有，输出默认style Css样式 */
        if (styleClass == null || styleClass.equals("")) {
            pageHtml.append("\r\n<style>\r\n");
            pageHtml.append("/* Pages Main Tyle */\r\n");
            pageHtml.append(".pages {\r\n");
            pageHtml
                    .append("    color: #000000;\r\n    cursor: default;\r\n    font-size: 12px;\r\n    font-family: Tahoma, Verdana;\r\n    padding: 3px 0px 0px 0px;\r\n    margin: 0px;\r\n");
            pageHtml.append("}\r\n");
            pageHtml
                    .append(".pages .count, .pages .number, .pages .arrow {\r\n");
            pageHtml
                    .append("    color: #000000 ;\r\n    font-size: 12px;\r\n    background-color: #F7F7F7;\r\n    border: 1px solid #CCCCCC;\r\n");
            pageHtml.append("}\r\n");
            pageHtml.append("/* Page and PageCount Style */\r\n");
            pageHtml.append(".pages .count {\r\n");
            pageHtml
                    .append("    font-weight: bold;\r\n    border-right: none;\r\n    padding: 2px 12px 3px 12px;\r\n");
            pageHtml.append("}\r\n");
            pageHtml.append("/* Mode 0,1,2 Style (Number) */\r\n");
            pageHtml.append(".pages .number {\r\n");
            pageHtml
                    .append("    font-weight: normal;\r\n    padding: 2px 12px 3px 12px;\r\n");
            pageHtml.append("}\r\n");
            pageHtml.append(".pages .number a, .pages .number span {\r\n");
            pageHtml.append("    font-size: 12px;\r\n");
            pageHtml.append("}\r\n");
            pageHtml.append(".pages .number span {\r\n");
            pageHtml
                    .append("    color: #999999;\r\n    margin: 0px 3px 0px 3px;\r\n");
            pageHtml.append("}\r\n");
            pageHtml.append(".pages .number a {\r\n");
            pageHtml
                    .append("    color: #000000;\r\n    text-decoration: none;\r\n");
            pageHtml.append("}\r\n");
            pageHtml.append(".pages .number a:hover {\r\n");
            pageHtml.append("    color: #0000ff;\r\n");
            pageHtml.append("}\r\n");
            pageHtml.append("/* Mode 3 Style (Arrow) */\r\n");
            pageHtml.append(".pages .arrow {\r\n");
            pageHtml
                    .append("    font-weight: normal;\r\n    padding: 0px 5px 0px 5px;\r\n");
            pageHtml.append("}\r\n");
            pageHtml.append(".pages .arrow a, .pages .arrow span {\r\n");
            pageHtml
                    .append("    font-size: 12px;\r\n    font-family: Webdings;\r\n");
            pageHtml.append("}\r\n");
            pageHtml.append(".pages .arrow span {\r\n");
            pageHtml
                    .append("    color: #999999;\r\n    margin: 0px 5px 0px 5px;\r\n");
            pageHtml.append("}\r\n");
            pageHtml.append(".pages .arrow a {\r\n");
            pageHtml
                    .append("    color: #000000;\r\n    text-decoration: none;\r\n");
            pageHtml.append("}\r\n");
            pageHtml.append(".pages .arrow a:hover {\r\n");
            pageHtml.append("    color: #0000ff;\r\n");
            pageHtml.append("}\r\n");
            pageHtml.append("/* Mode 4 Style (Select) */\r\n");
            pageHtml.append(".pages select, .pages input {\r\n");
            pageHtml
                    .append("    color: #000000;\r\n    font-size: 12px;\r\n    font-family: Tahoma, Verdana;\r\n");
            pageHtml.append("}\r\n");
            pageHtml.append("/* Mode 5 Style (Input) */\r\n");
            pageHtml
                    .append(".pages .input input.ititle, .pages .input input.itext, .pages .input input.icount {\r\n");
            pageHtml
                    .append("    color: #666666;\r\n    font-weight: bold;\r\n    background-color: #F7F7F7;\r\n    border: 1px solid #CCCCCC;\r\n");
            pageHtml.append("}\r\n");
            pageHtml.append(".pages .input input.ititle {\r\n");
            pageHtml
                    .append("    width: 70px;\r\n    text-align: right;\r\n    border-right: none;\r\n");
            pageHtml.append("}\r\n");
            pageHtml.append(".pages .input input.itext {\r\n");
            pageHtml
                    .append("    width: 25px;\r\n    color: #000000;\r\n    text-align: right;\r\n    border-left: none;\r\n    border-right: none;\r\n");
            pageHtml.append("}\r\n");
            pageHtml.append(".pages .input input.icount {\r\n");
            pageHtml
                    .append("    width: 35px;\r\n    text-align: left;\r\n    border-left: none;\r\n");
            pageHtml.append("}\r\n");
            pageHtml.append(".pages .input input.ibutton {\r\n");
            pageHtml
                    .append("    height: 20px;\r\n    color: #FFFFFF;\r\n    font-weight: bold;\r\n    font-family: Verdana;\r\n    background-color: #999999;\r\n    border: 1px solid #666666;\r\n    padding: 0px 0px 2px 1px;\r\n    margin-left: 2px;\r\n    cursor: hand;\r\n");
            pageHtml.append("}\r\n");

            // pageHtml.append("/* body */\r\n");
            // pageHtml.append("body {\r\n");
            // pageHtml.append(" font-size: 12px;\r\n");
            // pageHtml.append("}\r\n");
            pageHtml.append("</style>\r\n");

        }

        pageHtml.append("<div id=\"pages_" + this.formName + "_"
                + this.showTimes + "\" class=\""
                + (styleClass.equals("") ? "pages" : styleClass) + "\">\r\n");
        // 跳转脚本
        pageHtml.append("<script language=\"JavaScript\">\r\n");
        pageHtml.append("<!--\r\n");
        pageHtml.append("function ").append(formName + "_" + showTimes).append(
                "_toPage(page){ \r\n");
        pageHtml.append("    var turnTo = 1;\r\n");
        pageHtml.append("    if (typeof(page) == 'object') {\r\n");
        pageHtml
                .append("        turnTo = page.options[page.selectedIndex].value;\r\n");
        pageHtml.append("    } else {\r\n");
        pageHtml.append("        turnTo = page;\r\n");
        pageHtml.append("    }\r\n");

        pageHtml.append("    document." + formName + "_" + showTimes
                + ".pageSize.value = turnTo;\r\n");
        pageHtml.append("    document." + formName + "_" + showTimes
                + ".submit();\r\n");
        pageHtml.append("    return ;\r\n");
        pageHtml.append("}\r\n");
        pageHtml.append("function ").append(formName + "_" + showTimes).append(
                "_formatInputPage(e){ //限定输入页数格式\r\n");
        pageHtml
                .append("    var ie = navigator.appName==\"Microsoft Internet Explorer\"?true:false;\r\n");
        pageHtml.append("    if(!ie) var key = e.which;\r\n");
        pageHtml.append("    else var key = event.keyCode;\r\n");
        pageHtml
                .append("    if (key == 8 || key == 46 || (key >= 48 && key <= 57)) return true;\r\n");
        pageHtml.append("    return false;\r\n");
        pageHtml.append("}\r\n");
        pageHtml.append("-->\r\n");
        pageHtml.append("\r\n</script>\r\n");

        pageHtml.append("<form name=\"").append(this.formName).append("_")
                .append(this.showTimes).append("\" action=\"").append(
                        this.target).append("\"").append(
                        " method=\"post\">\r\n");

        pageHtml.append("<input type=\"hidden\" name=\"" + "pageSize"
                + "\" value=\"" + "" + "\"/>\r\n");
        // 判断是否有另外的附加参数
        if (parameterValue != null && parameterName != null) {
            pageHtml.append("<input type=\"hidden\" name=\"" + parameterName
                    + "\" value=\"" + parameterValue + "\"/>\r\n");
        }

        if (parameterMap != null && parameterMap.size() != 0) {
            Set keySet = parameterMap.keySet();
            Iterator iter = keySet.iterator();
            while (iter.hasNext()) {
                String paramerName = (String) iter.next();
                pageHtml.append("<input type=\"hidden\" name=\"" + paramerName
                        + "\" value=\"" + parameterMap.get(paramerName)
                        + "\"/>\r\n");
            }
        }
        pageHtml.append(this.getPageHtml(target, mode));
        pageHtml.append("\r\n</form>\r\n</div>\r\n");
        pageHtml.append("<!-- 分页区结束 -->");
        this.showTimes++;
        return pageHtml.toString();
    }

    public int getNextPage() {
        // return (this.curPage + 1)>=this.maxPage?this.maxPage:this.curPage+1;
        return this.curPage + 1;
    }

    public int getPreviousPage() {
        // return (this.curPage - 1)<=0?1:this.curPage-1;
        return this.curPage - 1;
    }

    public boolean hasNextPage() {
        if (this.curPage < this.maxPage) {
            return true;
        } else {
            return false;
        }
    }

    public boolean hasPreviousPage() {
        if (this.curPage - 1 > 0) {
            return true;
        } else {
            return false;
        }
    }

    public int getShowTimes() {
        return showTimes;
    }

    public String getStyleClass() {
        return styleClass;
    }

    public void setStyleClass(String styleClass) {
        this.styleClass = styleClass;
    }

    public int getMode() {
        return mode;
    }

    public void setMode(int mode) {
        this.mode = mode;
    }

    /**
     * 打印 Page 对象的当前信息
     */
    public String toString() {
        return "当前是第 " + curPage + " 页;共有 " + maxPage + " 页,共 " + maxRowCount
                + " 条记录(行);每页 " + rowsPerPage + " 条记录(行)";
    }

    public static void main(String[] args) {
        Page page = new Page();
        /*
         * Map tempMap = new HashMap(); tempMap.put("key1", "aaaaaa");
         * tempMap.put("key2", "bbbbbb"); page.setParameterMap(tempMap); //
         * page.setParameterName("keyWords"); //
         * page.setParameterValue("圣斗士星矢"); // page.setParameterName(""); //
         * page.setParameterValue("");
         * System.out.println(page.getPageStr("/xxx.jsp"));
         * 
         */
        page.setMode(1);
        page.setMaxRowCount(300);
        page.setCurPage(1);
        page.setRowsPerPage(15);
        page.initialize();

        Map tempMap = new HashMap();
        tempMap.put("key1", "aaaaaa");
        tempMap.put("key2", "bbbbbb");
        page.setParameterMap(tempMap);
        page.setTarget("/xxx.jsp");

        System.out.println(page.getPageHtml());

        System.out.println(page);

        System.out.println(page.getPageStr());

        System.out.println(page.getPageHtml());
    }

	public boolean isAJAX() {
		return isAJAX;
	}

	public void setAJAX(boolean isAJAX) {
		this.isAJAX = isAJAX;
	}

    public String getJavascriptFunctionName() {
		return javascriptFunctionName;
	}

	public void setJavascriptFunctionName(String javascriptFunctionName) {
		this.javascriptFunctionName = javascriptFunctionName;
	}

	public void setMaxPage(int maxPage) {
		this.maxPage = maxPage;
	}
}
